/**
 *
 * Copyright by Samuel Castro & Joaquim Fonseca, 2011
 * All Rights Reserved
 *
 * NOTES:
 * Please do not redistribute this plugin without my permission
 * Plugin made for private use only
 *
 */
package com.SySammy.sdQuests;

import com.SySammy.sdQuests.QuestMaker.QuestPlayerListener;
import com.SySammy.sdQuests.QuestMaker.sdEntityListener;
import com.SySammy.sdQuests.prop.QuestsProperties;
import com.nijiko.permissions.PermissionHandler;
import com.nijikokun.bukkit.Permissions.Permissions;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;

public class sdQuests extends JavaPlugin {

    private static final String QuestsDIR = "plugins/RPGmod/Quests/";
    private static final Logger log = Logger.getLogger("Minecraft");
    public PermissionHandler Permissions;
    private boolean UsePermissions;
    QuestPlayerListener QPL = new QuestPlayerListener();
    sdEntityListener EntityListener = new sdEntityListener();

    @Override
    public void onLoad() {
        new File(QuestsDIR).mkdirs();
        File QuestMaker = new File(QuestsDIR + "QuestMaker.yml");
        if (!QuestMaker.exists()) {
            InputStream jarURL = getClass().getResourceAsStream("/QuestTemplate/QuestMaker.yml");
            try {
                copyFile(jarURL, new File(QuestsDIR + "QuestMaker.yml"));

            } catch (Exception ex) {
                Logger.getLogger(sdQuests.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void main(String[] args) {
        // TODO code application logic here
    }

    public void onDisable() {
        System.out.println("disabled, goodbye wold");
    }

    public void onEnable() {
        PluginDescriptionFile pdfFile = this.getDescription();
        PluginManager pm = getServer().getPluginManager();
        QuestsProperties.loadMiningProps();
        setupPermissions();
        getCommand("quest").setExecutor(new sdQuestsCmds(this));
        System.out.println(pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!");
        pm.registerEvent(Event.Type.ENTITY_DAMAGE, this.EntityListener, Event.Priority.Normal, this);
        pm.registerEvent(Event.Type.PLAYER_KICK, this.QPL, Event.Priority.Normal, this);
        pm.registerEvent(Event.Type.PLAYER_QUIT, this.QPL, Event.Priority.Normal, this);
    }

    public static void copyFile(InputStream in, File out) throws Exception {
        InputStream fis = in;
        FileOutputStream fos = new FileOutputStream(out);
        try {
            byte[] buf = new byte[1024];
            int i = 0;
            while ((i = fis.read(buf)) != -1) {
                fos.write(buf, 0, i);
            }
        } catch (Exception e) {
            throw e;
        } finally {
            if (fis != null) {
                fis.close();
            }
            if (fos != null) {
                fos.close();
            }
        }
    }

    private void setupPermissions() {
        Plugin test = this.getServer().getPluginManager().getPlugin("Permissions");

        if (this.Permissions == null) {
            if (test != null) {
                UsePermissions = true;
                this.Permissions = ((Permissions) test).getHandler();
                System.out.println("Permissions is enable for sdBountyHunter.");
            } else {
                log.info("Permission system not detected, defaulting to OP");
                UsePermissions = false;
            }
        }
    }

    public boolean casUseQuestsList(Player p) {
        if (UsePermissions) {
            return this.Permissions.has(p, "quests.list");
        }
        return p.isOp();
    }

    public boolean casUseQuestsInfo(Player p) {
        if (UsePermissions) {
            return this.Permissions.has(p, "quests.info");
        }
        return p.isOp();
    }

    public boolean casUseQuestsStart(Player p) {
        if (UsePermissions) {
            return this.Permissions.has(p, "quests.start");
        }
        return p.isOp();
    }

    public boolean casUseQuestsEnd(Player p) {
        if (UsePermissions) {
            return this.Permissions.has(p, "quests.end");
        }
        return p.isOp();
    }

    public boolean casUseQuestsHelp(Player p) {
        if (UsePermissions) {
            return this.Permissions.has(p, "quests.help");
        }
        return p.isOp();
    }
}
